package Java8实战._2_Stream_使用流._4_归约;

import java.util.Arrays;
import java.util.List;

public class _1_元素求和 {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3,4,5);

        //====================
        // reduce 两个参数
        //====================
        //Lambda
        int sum = list.stream()
                .reduce(0, (a, b) -> a + b);
        System.out.println(sum);

        //方法引用
        int sum1 = list.stream()
                .reduce(0,Integer::sum);
        System.out.println(sum1);

        //====================
        // reduce 一个参数
        //====================
        list.stream()
                .reduce((Integer a,Integer b) -> a + b)
                .ifPresent(System.out::println);

    }
}
